home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 44
/
Amiga Format CD44 (1999-08-26)(Future Publishing)(GB)(Track 1 of 3)[!][issue 1999-10].iso
/
-serious-
/
misc
/
calculator
/
window.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-07-12
|
13KB
|
338 lines
/* Window.c */
/* This file contains the main execution loop and all code for handling
program functions */
#include <exec/types.h>
#include <clib/exec_protos.h>
#include <intuition/intuition.h>
#include <intuition/IntuitionBase.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>
#include <graphics/gfxmacros.h>
#include <graphics/GfxBase.h>
#include <clib/dos_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "Gadgets.c"
/*
** Function for defining and displaying About Alert message.
*/
void about_alert()
{
char message[156];
strcpy(message, " Calculator program created by Donald W Millican.");
strcat(message,
" (C) Copyright 1999 DWM Productions.");
strcat(message,
" Version 1.2 16/06/99. Press Left Mouse Button to continue.");
message[0]=0; /* X position of the first string */
message[1]=32; /* - " - */
message[2]=16; /* Y - " - */
message[51]='\0'; /* NULL sign which finish of the first string. */
message[52]=TRUE; /* Continuation byte set to TRUE (new string). */
message[53]=0;
message[54]=32;
message[55]=32;
message[91]='\0';
message[92]=TRUE;
message[93]=0; /* X position of the third string. */
message[94]=32; /* - " - */
message[95]=48; /* Y - " - */
message[154]='\0'; /* NULL sign which finish of the third string. */
message[155]=FALSE; /* Continuation byte set to FALSE (last string). */
/* We will now display the Alert message: */
DisplayAlert( RECOVERY_ALERT, message, 64 );
}
/*
** Standard message handling loop with GadTools message handling functions
** used (GT_GetIMsg() and GT_ReplyIMsg()).
*/
void process_window_events(struct Screen *mysc, struct Window *mywin,
struct Gadget *my_gads[], struct Menu *mainmenustrip, struct StringInfo *gad_ptr[])
{
struct IntuiMessage *imsg;
ULONG imsgClass;
UWORD imsgCode;
BOOL terminated = FALSE;
struct Gadget *gad;
UWORD menu_number, which_menu, which_item;
MenuItem *item;
int total = 0;
int entered_number = 0, no_figures = 0, sum = 0;
BOOL special_button = FALSE;
BOOL add = FALSE, subtract = FALSE, multiply = FALSE, divide = FALSE;
while (!terminated)
{
Wait (1 << mywin->UserPort->mp_SigBit);
/* GT_GetIMsg() returns an IntuiMessage with more friendly information for
** complex gadget classes. Use it wherever you get IntuiMessages where
** using GadTools gadgets.
*/
while ((!terminated) &&
(imsg = GT_GetIMsg(mywin->UserPort)))
{
/* Presuming a gadget, of course, but no harm...
** Only dereference this value (gad) where the Class specifies
** that it is a gadget event.
*/
gad = (struct Gadget *)imsg->IAddress;
imsgClass = imsg->Class;
imsgCode = imsg->Code;
menu_number = imsg->Code;
/* Use the toolkit message-replying function here... */
GT_ReplyIMsg(imsg);
switch (imsgClass)
{
/* --- WARNING --- WARNING --- WARNING --- WARNING --- WARNING ---
** GadTools puts the gadget address into IAddress of IDCMP_MOUSEMOVE
** messages. This is NOT true for standard Intuition messages,
** but is an added feature of GadTools.
*/
case IDCMP_VANILLAKEY:
case IDCMP_GADGETUP:
special_button = handleGadgetEvent(mysc, mywin, gad, imsgCode, my_gads, entered_number, no_figures);
if(special_button == FALSE)
{
if(no_figures == 0)
{
total = entered_number;
GT_SetGadgetAttrs(my_gads[MYGAD_DISPLAY], mywin, NULL, GTNM_Number, total, TAG_END);
}
if(no_figures >= 1 && no_figures <= 10)
{
if(entered_number == 10)
{
total *= 10;
} else
{
total *= 10;
total += entered_number;
}
GT_SetGadgetAttrs(my_gads[MYGAD_DISPLAY], mywin, NULL, GTNM_Number, total, TAG_END);
}
} else // if special button == TRUE
{
if(gad->GadgetID == MYGAD_CLEAR)
{
sum = 0;
total = 0;
no_figures = 0;
add = subtract = multiply = divide = FALSE;
GT_SetGadgetAttrs(my_gads[MYGAD_DISPLAY], mywin, NULL, GTNM_Number, 0, TAG_END);
}
if(gad->GadgetID == MYGAD_ADD || imsgCode == '+')
{
if(no_figures > 0 && no_figures < 10)
{
if(subtract == TRUE)
{
sum -= total;
subtract = FALSE;
add = TRUE;
} else
if(add == TRUE)
{
sum += total;
add = TRUE;
subtract = FALSE;
} else
{
sum += total;
add = TRUE;
subtract = FALSE;
}
total = 0;
no_figures = 0;
entered_number = 0;
GT_SetGadgetAttrs(my_gads[MYGAD_DISPLAY], mywin, NULL, GTNM_Number, sum, TAG_END);
}
}
if(gad->GadgetID == MYGAD_SUBTRACT || imsgCode == '-')
{
if(no_figures > 0 && no_figures < 10)
{
if(add == TRUE)
{
sum += total;
add = FALSE;
subtract = TRUE;
} else
if(subtract == TRUE)
{
sum -= total;
subtract = TRUE;
add = FALSE;
} else
{
sum += total;
subtract = TRUE;
add = FALSE;
}
total = 0;
no_figures = 0;
entered_number = 0;
GT_SetGadgetAttrs(my_gads[MYGAD_DISPLAY], mywin, NULL, GTNM_Number, sum, TAG_END);
}
}
if(gad->GadgetID == MYGAD_MULTIPLY || imsgCode == '*')
{
if(no_figures > 0 && no_figures < 10)
{
if(divide == TRUE)
{
sum /= total;
divide = FALSE;
multiply = TRUE;
} else
if(multiply == TRUE)
{
sum *= total;
multiply = TRUE;
divide = FALSE;
} else
{
sum += total;
multiply = TRUE;
divide = FALSE;
}
total = 0;
no_figures = 0;
entered_number = 0;
GT_SetGadgetAttrs(my_gads[MYGAD_DISPLAY], mywin, NULL, GTNM_Number, sum, TAG_END);
}
}
if(gad->GadgetID == MYGAD_DIVIDE || imsgCode == '/')
{
if(no_figures > 0 && no_figures < 10)
{
if(multiply == TRUE)
{
sum *= total;
multiply = FALSE;
divide = TRUE;
} else
if(divide == TRUE)
{
sum /= total;
divide = TRUE;
multiply = FALSE;
} else
{
sum += total;
divide = TRUE;
multiply = FALSE;
}
total = 0;
no_figures = 0;
entered_number = 0;
GT_SetGadgetAttrs(my_gads[MYGAD_DISPLAY], mywin, NULL, GTNM_Number, sum, TAG_END);
}
}
if(gad->GadgetID == MYGAD_CHANGE_SIGN)
{
if(total > 0)
{
total = total - (total * 2);
} else if(total < 0)
{
total = total - (total * 2);
}
GT_SetGadgetAttrs(my_gads[MYGAD_DISPLAY], mywin, NULL, GTNM_Number, total, TAG_END);
}
if(gad->GadgetID == MYGAD_EQUALS)
{
if(no_figures < 11)
{
if(add == TRUE)
sum += total;
if(subtract == TRUE)
sum -= total;
if(multiply == TRUE)
sum *= total;
if(divide == TRUE)
sum /= total;
if(sum == 0)
GT_SetGadgetAttrs(my_gads[MYGAD_DISPLAY], mywin, NULL, GTNM_Number, total, TAG_END);
else
GT_SetGadgetAttrs(my_gads[MYGAD_DISPLAY], mywin, NULL, GTNM_Number, sum, TAG_END);
}
}
special_button = FALSE;
}
// Redraw gadgets to show updates
GT_BeginRefresh(mywin);
GT_EndRefresh(mywin, TRUE);
// reset gadget pointer
gad = NULL;
break;
case IDCMP_MENUPICK:
// Menu chosen
which_menu=MENUNUM(menu_number);
which_item=ITEMNUM(menu_number);
while( menu_number != MENUNULL )
{
/* Get the address of the item: */
item = (struct MenuItem *) ItemAddress( mainmenustrip, menu_number );
/* Check which item was selected: */
if( which_menu == 0 )
{
if( which_item == 0 )
{
// about selected
about_alert();
}
if( which_item == 1 )
{
terminated = TRUE;
}
}
/* Get the following item's menu number: */
menu_number = item->NextSelect;
}
break;
case IDCMP_CLOSEWINDOW:
terminated = TRUE;
break;
case IDCMP_REFRESHWINDOW:
/* With GadTools, the application must use GT_BeginRefresh()
** where it would normally have used BeginRefresh()
*/
GT_BeginRefresh(mywin);
GT_EndRefresh(mywin, TRUE);
break;
}
}
}
}